//
//  ST_MatchBeforeOptionsController.swift
//  btvideo
//
//  Created by mac on 2020/7/28.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_MatchBeforeOptionsController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 10+statusBarHeight, left: 0, bottom: -16, right: 0))
        }
        
        view.addSubview(topCloseBtn)
        topCloseBtn.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
        }
        
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(22)
        }
        
        root_view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.width.equalTo(44)
            make.height.equalTo(44)
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalTo(titleLabel)
        }
        
        root_view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 10+52, left: 0, bottom: 16, right: 0))
        }
        
        scrollView.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        
        contentView.addSubview(optionView0)
        optionView0.snp.makeConstraints { (make) in
            
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalToSuperview()
            make.height.equalTo(optionView0.snp.width).dividedBy(335/170.0)
            
        }
        
        let gap = TIANCompatibleValue(value: 10)
        
        contentView.addSubview(optionView1)
        optionView1.snp.makeConstraints { (make) in
            
            make.top.equalTo(optionView0.snp.bottom).offset(gap)
            make.left.right.height.equalTo(optionView0)
        }
        
        contentView.addSubview(optionView2)
        optionView2.snp.makeConstraints { (make) in
            
            make.top.equalTo(optionView1.snp.bottom).offset(gap)
            make.left.right.height.equalTo(optionView0)
            
        }
        
        
        contentView.addSubview(optionView3)
        optionView3.snp.makeConstraints { (make) in
            
            make.top.equalTo(optionView2.snp.bottom).offset(gap)
            
            make.left.right.equalTo(optionView0)
            
            make.height.equalTo(optionView3.snp.width).dividedBy(335/115.0)
            
            make.bottom.equalToSuperview().offset(iPhoneXSeries ? -BTSafeAreaBottom-30 : -45)
        }
        
        
        binding()
    }
    
    private func binding(){
        
        self.show_indeterminate_h_u_d()
        
        TIANSession(requestUrl: PH.getCoinUsageConfApi).withUnretained(self).subscribe(onNext: { own , result in
            
            switch result {
            case .success(let json):
                
                own.hidden_indeterminate_h_u_d()
                
                let data = json["data"]
                
                own.match_game_list = data["bump"].arrayValue.map { (itemJson) -> ST_FlashGameDataModel in
                    return ST_FlashGameDataModel.init(fromJson: itemJson)
                }
                
                
                own.match_game_list.last?.freeNum = data["user"]["free_bump_num"].intValue
                
                own.setupControlValue()
                
            case .failure(let error):
                
                own.toast(text: error.e)
            }
            
        }).disposed(by: rx.disposeBag)
        
        optionView0.action_closure = {[weak self] in
            self?.push_to_animate_controller(index: 0)
        }
        
        optionView1.action_closure = {[weak self] in
            self?.push_to_animate_controller(index: 1)
        }
        
        optionView2.action_closure = {[weak self] in
            self?.push_to_animate_controller(index: 2)
        }
        
        optionView3.action_closure = {[weak self] in
            self?.push_to_animate_controller(index: 3)
        }
        
    }
    
    private func setupControlValue(){
        
        if let gameModel = match_game_list.first{
            
            optionView0.model = ST_MatchBeforePropsItemViewModel.init(title: gameModel.name, location: SCComponets.location_component.current_address?.city ?? "", subTitle: gameModel.remark, price: gameModel.useCoin,iconUrl:gameModel.image ,backgroundImage: ST_ImageUtils.imageName("特质对对碰_icon_0")!, tintColor: UIColor.init(hex: 0xFFD30C)!)
            
        }
        
        if let gameModel = match_game_list[safe:1]{
            
            optionView1.model = ST_MatchBeforePropsItemViewModel.init(title: gameModel.name, location: nil, subTitle: gameModel.remark, price: gameModel.useCoin,iconUrl:gameModel.image ,backgroundImage: ST_ImageUtils.imageName("特质对对碰_icon_1")!, tintColor: UIColor.init(hex: 0x2CC6D3)!)
            
        }
        
        
        if let gameModel = match_game_list[safe:2]{
            
            optionView2.model = ST_MatchBeforePropsItemViewModel.init(title: gameModel.name, location: nil, subTitle: gameModel.remark, price: gameModel.useCoin,iconUrl:gameModel.image ,backgroundImage: ST_ImageUtils.imageName("特质对对碰_icon_2")!, tintColor: UIColor.init(hex: 0xFFA2C5)!)
            
        }
        
        if let gameModel = match_game_list[safe:3]{
            
            optionView3.model = ST_MatchBeforePropsNoImageItemViewModel.init(title: gameModel.name, subTitle: gameModel.remark, freeNumber: gameModel.freeNum, price: gameModel.useCoin, tintColor: UIColor.init(hex: 0xFFAA48)!, gradientColors: [UIColor.init(hex: 0xFFC95B)!,UIColor.init(hex: 0xFF8B36)!])
            
        }
        
    }
    
    fileprivate func push_to_animate_controller(index:Int){
        
        guard let gameModel = match_game_list[safe:index] , let coin = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sweet_coin  else{ return }
        
        
        if gameModel.freeNum < 1 , coin < gameModel.useCoin.float{
            
            
            switch index {
            case 0:
                ST_MaidWayManager.shared.source = .对对碰_同城
            case 1:
                ST_MaidWayManager.shared.source = .对对碰_颜值
            case 2:
                ST_MaidWayManager.shared.source = .对对碰_最匹配
            default:
                ST_MaidWayManager.shared.source = .对对碰_基础
            }
            
            ST_RechargeViewController.toast()
            
            
            return
        }
        
        
        
        self.goback_previous_controller(animated: false, completion: {
            
            
            let vc = ST_MatchingAnimationViewController()
            
            vc.title = "聊天速配"
            
            vc.supplementId = gameModel.id.string
            
            vc.matchSetting = (matchTitle:"正在匹配中...",content:"伴心酱正在帮你寻找在线特质最匹配的人呢\n请耐心等待哟~")
            
            let nav = UINavigationController.create_hiding_bar_navigation_controller(rootViewController: vc)
            
            nav.interactivePopGestureRecognizer?.isEnabled = false
            
            GlobalPresent(toController: nav, animated: false)
            
            
        })
    }
    
    //    MARK: - Properties
    private lazy var  match_game_list = [ST_FlashGameDataModel]()
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 16
        return control
    }()
    
    fileprivate lazy var scrollView:UIScrollView = {
        let control = UIScrollView()
        control.backgroundColor = .clear
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        return control
    }()
    
    fileprivate lazy var contentView:UIView = {
        let control = UIView()
        control.backgroundColor = .clear
        return control
    }()
    
    fileprivate lazy var topCloseBtn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var close_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("特质引导_icon_关闭")!)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "特质对对碰"
        control.textColor = UIColor(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var optionView0:ST_MatchBeforePropsItemView = {
        
        let control = ST_MatchBeforePropsItemView()
        return control
    }()
    
    fileprivate lazy var optionView1:ST_MatchBeforePropsItemView = {
        
        let control = ST_MatchBeforePropsItemView()
        
        
        
        return control
    }()
    
    
    fileprivate lazy var optionView2:ST_MatchBeforePropsItemView = {
        
        let control = ST_MatchBeforePropsItemView()
        return control
    }()
    
    
    fileprivate lazy var optionView3:ST_MatchBeforePropsNoImageItemView = {
        
        let control = ST_MatchBeforePropsNoImageItemView()
        
        return control
    }()
    
    
}
extension ST_MatchBeforeOptionsController{
    
    @objc fileprivate func cancel_handle(){
        
        goback_previous_controller()
    }
    
}


